package com.kaibes.module.file.local;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaibes.module.file.local.db.FileInfo;
import com.kaibes.module.file.local.db.FileInfoService;
import com.kaibes.module.file.local.db.FileMark;
import com.kaibes.module.file.local.db.FileMarkService;

@Service
public class FileTransaction {

    @Autowired
    private FileInfoService fileInfoService;
    @Autowired
    private FileMarkService fileMarkService;

    @Transactional
    public boolean saveFile(FileInfo fileInfo, String uuid, Integer userId) {
        List<Number> ids = fileInfoService.save(fileInfo);
        if (ids != null) {
            FileMark fileMark = new FileMark();
            fileMark.setFileInfoId(fileInfo.getId());
            fileMark.setUuid(uuid);
            fileMark.setName(fileInfo.getName());
            fileMark.setUserId(userId);
            fileMark.setSuffix(fileInfo.getSuffix());
            fileMark.setType(fileInfo.getType());
            ids = fileMarkService.save(fileMark);
        }
        return ids != null;
    }

}
